package g11.testdao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import g11.dao.DAOException;
import g11.dao.PatientDAO;
import g11.dao.PatientDAOImpl;
import g11.dao.UserDAO;
import g11.dao.UserDAOImpl;
import g11.model.Patient;
import g11.model.User;

import java.util.Date;

import org.junit.Test;

public class testPatientDao {
	// test create, retrieve, update and delete
	@SuppressWarnings("deprecation")
	@Test
	public void testPatientCRUD() throws DAOException {
		// initiate object
		Patient patient = new Patient();
		patient.setUsername("penny");
		patient.setName("Penny Jen");
		patient.setPassword("1234");
		Byte type = 0;
		patient.setType(type);
		patient.setPhone("519-888-8888");
		patient.setAddress("77 Weber Street");
		patient.setOhip("2222222222ka");
		patient.setSin("1111111111");
		patient.setCurrentHealth("Diabetes.");
		// to form a DateTime of joda typee
		Date date = new Date(111, 12, 8);
		java.sql.Date myDate = new java.sql.Date(date.getTime());
		patient.setLastVisitTime(myDate);
		System.out.println(myDate.toString());
		// patient.setDepartment("Surgery");
		// patient.setTitle("Surgery Doctor");
		// create start
		PatientDAO patientDao = new PatientDAOImpl();
		patientDao.createPatient(patient);
		Patient retrievedpatient = new Patient();
		System.out.println("done.created a new patient, now getPatientByName");
		// create a temp patient
		retrievedpatient = patientDao.getPatientByName("Penny Jen");
		assertNotNull(retrievedpatient);
		System.out.println("get a patient by sin");
		assertEquals("1111111111", retrievedpatient.getSin());
		System.out.println("get a patient by ohip");
		assertEquals("2222222222ka", retrievedpatient.getOhip());
		System.out.println("get a patient by lastVisitTime");
		// assertEquals(myDate, retrievedpatient.getLastVisitTime());

		UserDAO userDao = new UserDAOImpl();
		User retrievedUser = new User();
		retrievedUser = userDao.getUserbyUsername("penny", "1234", type);
		// assertEquals("penny", retrievedUser.getUsername());
		// assertEquals("1234", retrievedUser.getPassword());
		// assertEquals(type, (Byte)retrievedUser.getType());
		assertEquals("Penny Jen", retrievedUser.getName());

		// create end
		// update start
		retrievedpatient.setName("Toonie Jen");
		retrievedpatient.setPhone("519-888-8888");
		System.out.println("update a patient");
		patientDao.updatePatient(retrievedpatient);
		System.out.println("get a patient by lastVisitTime");
		retrievedpatient = patientDao.getPatientByLastVisitTime(myDate);
		assertNotNull(retrievedpatient);
		assertEquals("2222222222ka", retrievedpatient.getOhip());
		assertEquals("Toonie Jen", retrievedpatient.getName());
		// update end

		// to test getPtientbyID
		int pid1 = retrievedpatient.getId();
		System.out.println("before getPatientbyId" + pid1);
		retrievedpatient = patientDao.getPatientById(pid1);
		int pid2 = retrievedpatient.getId();
		System.out.println(" getPatientbyId" + pid2);

		// delete start
		System.out.println("delete a patient");
		patientDao.deletePatient(retrievedpatient);
		retrievedpatient = patientDao.getPatientByName("Toonie Jen");
		assertNull(retrievedpatient);
		// delete end
	}

	/*
	 * public Date setDate(String s){
	 * 
	 * CalendarComparator c=new CalendarComparator(); Date d; try { d = (Date)
	 * df.parse(s); } catch (ParseException e) { // TODO Auto-generated catch
	 * block e.printStackTrace(); } return d; }
	 */
}
